package com.selen.mapper;

import com.selen.entities.Dept;
import com.selen.entities.Room;
import com.selen.entities.School;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Selen
 */
@Repository
public interface DeptMapper {

    @Insert("insert into ums_dept " +
            "(`title`,  `info`, `fk_room_id`, `deleted`, `created`, `updated`) " +
            "values (#{title}, #{info}, #{fkRoomId}, #{deleted}, #{created}, #{updated})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Dept dept);

    @Select("select ud.id, ud.title, ud.info, ud.fk_room_id, ud.deleted, ud.created, ud.updated " +
            "from ums_dept as ud " +
            "where ud.id = #{param1} and ud.deleted = 0")
    Dept selectById(Long id);

    @Update("<script>" +
            "update ums_dept " +
            "<set> " +
            "<if test = 'title != null'> title = #{title}, </if> " +
            "<if test = 'info != null'> info = #{info}, </if> " +
            "<if test = 'fkRoomId != null'> fk_room_id = #{fkRoomId}, </if> " +
            "<if test = 'deleted != null'> deleted = #{deleted}, </if> " +
            "<if test = 'created != null'> created = #{created}, </if> " +
            "<if test = 'updated != null'> updated = #{updated}, </if> " +
            " </set> " +
            "where id = #{id} and deleted = 0 " +
            "</script>")
    int updateById(Dept dept);

    @Update("update ums_dept set deleted = 1, updated = now() " +
            "where id = #{param1}")
    int logicDeleteById(Long id);

    @Update("<script>" +
            "update ums_dept set deleted = 1, updated = now() " +
            "where " +
            "<foreach collection = 'list' item = 'e' open = 'id in (' close = ')' separator = ',' > " +
            "${e}" +
            " </foreach>" +
            "</script>")
    int logicDeleteByIds(List<Long> ids);

    @Results(id = "resultMap", value = {
            @Result(column = "id", property = "id", id = true),
            @Result(column = "fk_room_id", property = "fkRoomId"),
            @Result(column = "fk_room_id", property = "room", one = @One(select = "com.selen.mapper.RoomMapper.selectById"))
    })
    @Select("select ud.id, ud.title, ud.info, ud.fk_room_id, ud.deleted, ud.created, ud.updated " +
            "from ums_dept as ud " +
            "where ud.deleted = 0 ")
    List<Dept> selectAll();

    @Delete("delete from ums_dept where id = #{param1}")
    int deleteById(Long id);

    @Select("select count(*) > 0  " +
            "from ums_dept as ud " +
            "where ud.fk_room_id = #{param1} and ud.deleted = 0")
    Boolean existByRoomId(Long fkRoomId);

    @Select("<script>" +
            "select count(*) > 0 " +
            "from ums_dept as ud " +
            "where " +
            "<foreach item='e' collection='list' open='ud.fk_room_id in (' close=')' separator=',' > " +
            "#{e} " +
            "</foreach> " +
            "and ud.deleted = 0" +
            "</script>")
    Boolean existByRoomIds(List<Long> fkRoomId);

    @ResultMap("resultMap")
    @Select("select ud.id, ud.title, ud.info, ud.fk_room_id, ud.deleted, " +
            "ud.created, ud.updated  " +
            "from ums_dept as ud " +
            "where ud.title like concat('%', #{param1}, '%') and ud.deleted = 0")
    List<Dept> selectByDimName(String keyword);

}
